<template>
  <div>
    <div class="query-card">
      <div class="grid-content bg-purple search">
        <el-form ref="dialogquery" :model="dialogquery" label-width="100px" style="display:flex;">
          <el-form-item style="margin-bottom:0px" label="商品名称">
            <el-input style="width:260px" v-model="dialogquery.keywords" placeholder="请输入商品名称"></el-input>
          </el-form-item>
          <el-form-item style="margin-bottom:0px" label="商品分类">
            <el-cascader
              v-model="dialogquery.class_id"
              :options="dialogoptions"
              :props="{ checkStrictly: true,value:'id',label:'class_name' }"
              placeholder="请选择奖品分类"
              clearable
              :loading="queryLoading"
            ></el-cascader>
          </el-form-item>
        </el-form>
      </div>
      <div class="grid-content bg-purple-light" style="text-align:right">
        <el-button type="primary" @click="dialogqueryAsk">查询</el-button>
        <el-button @click="dialogqueryReset">重置</el-button>
      </div>
    </div>
    <el-table
      :data="goodsData"
      ref="sendmultipleTable"
      border
      tooltip-effect="dark"
      style="width: 100%;margin: 20px 0;"
      @row-click="handleSelectionChange"
      :header-cell-style="{background:'#eee'}"
    >
      <el-table-column align="center" width="55">
        <template slot-scope="scope">
          <el-radio v-model="sendForm.goods_id" :label="scope.row.id">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>

      <el-table-column align="center" prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column align="center" label="商品图片">
        <template slot-scope="scope">
          <el-popover placement="top-start" title trigger="hover">
            <img :src="scope.row.img" alt style="max-width:150px" />
            <el-avatar
              slot="reference"
              shape="square"
              :size="50"
              fit="scale-down"
              :src="scope.row.img"
            ></el-avatar>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="class_name" label="分类"></el-table-column>
      <el-table-column align="center" prop="price" label="单价"></el-table-column>
    </el-table>
    <div style="text-align:right">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="dialogcurrentPage"
        :total="dialogtotal"
        @current-change="dialogcurrentChange"
      ></el-pagination>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="goodsInfo">确 定</el-button>
    </span>
  </div>
</template>

<script>
import { boxRewardList } from '@/api/boxreward'
import { categoryList } from '@/api/product'

export default {
  data() {
    return {
      dialogcurrentPage: 1,
      dialogpageSize: 10,
      dialogtotal: 0,
      dialogoptions: [],
      queryLoading: false,
      dialogquery: {
        keywords: '',
        class_id: ''
      },
      sendForm: { goods_id: '' },
      goodsData: []
    }
  },
  computed: {},
  created() {
    this.getRewardList()
    this.getCategary()
  },
  methods: {
    // 获取商品分类
    async getCategary() {
      this.queryLoading = true
      const res = await categoryList({ page: 1, limit: 999, keywords: '' })
      console.log(res)
      this.dialogoptions = res.data
      this.queryLoading = false
    },
    async getRewardList() {
      const res = await boxRewardList({
        page: this.dialogcurrentPage,
        limit: this.dialogpageSize,
        keywords: this.dialogquery.keywords,
        class_id: this.dialogquery.class_id
      })
      this.goodsData = res.data
      this.dialogtotal = res.count
    },
    cancel() {
      this.$emit('singCancel')
    },
    goodsInfo() {
      this.$emit('getPayGood', this.sendForm)
      // this.form.goods_id = this.goods.id
      // this.form.goods_name = this.goods.goods_name
      // this.form.price = this.goods.price
      // this.form.img = this.goods.img
      // this.dialogVisible = false
    },
    dialogqueryAsk() {
      this.dialogcurrentPage = 1
      this.dialogquery.class_id =
        this.dialogquery.class_id[this.dialogquery.class_id.length - 1]
      this.getRewardList()
    },
    //重置
    dialogqueryReset() {
      this.resetForm('dialogquery')
      this.dialogtotal = 0
      this.dialogcurrentPage = 1
      this.dialogpageSize = 10
      this.dialogquery.keywords = ''
      this.dialogquery.class_id = ''
      this.getRewardList()
    },
    handleSelectionChange(val) {
      console.log(val)
      this.goods = val
      this.sendForm = Object.assign({}, val, {
        goods_id: val.id
      })
    },
    //分页改变
    dialogcurrentChange(current) {
      this.dialogcurrentPage = current
      this.getRewardList()
    }
  }
}
</script>
<style lang="scss" scoped>
.query-card {
  display: flex;
  justify-content: space-between;
}
</style>
